package vn.orderonline.web.admin.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import vn.orderonline.com.core.business.dto.FoodDto;
import vn.orderonline.com.core.business.food.IFood;
import vn.orderonline.com.core.business.menufood.IMenuFood;
import vn.orderonline.com.core.business.restaurant.IRestaurantManager;

@Controller
@RequestMapping("/food")
public class FoodController {

	private static final Logger LOGGER = Logger.getLogger(FoodController.class);

	@Autowired
	private IFood iFood;
	@Autowired
	private IMenuFood iMenuFood;
	@Autowired
	private IRestaurantManager iRestaurantManager;

	@RequestMapping(value = "/list", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	public String getListFood(@RequestParam Integer restaurantId,
			@RequestParam Integer menuFoodId, ModelMap model, HttpServletRequest request) {
		LOGGER.info("START GET LIST FOOD!");
		FoodDto food = null;
		try {
			food = new FoodDto();
			food.setRestaurantId(restaurantId);
			food.setRestaurantName(iRestaurantManager.getRestaurantNameByRestaurantId(restaurantId));
			food.setMenuFoodId(menuFoodId);
			food.setMenuFoodName(iMenuFood
					.getMenuFoodNameByMenuFoodId(menuFoodId));
			model.addAttribute("food", food);
			model.addAttribute("list", iFood.getAllFood(restaurantId, menuFoodId));
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN GET LIST FOOD!", e);
		}
		LOGGER.info("END GET LIST FOOD!");
		return "/food/searchfood";
	}

	@RequestMapping(value = "/searchfood", method = RequestMethod.POST, produces = { "appcliation/json;charset=UTF-8" })
	public String searchFood(@ModelAttribute FoodDto food, ModelMap model) {
		LOGGER.info("START SEARCH FOOD!");
		try {
			model.addAttribute("food", food);
			model.addAttribute("list", iFood.searchFood(food));
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN SEACH FOOD!", e);
		}
		LOGGER.info("END SEARCH FOOD!");
		return "/food/searchfood";
	}

	@RequestMapping(value = "/addform", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	public String createFormAdd(@RequestParam Integer restaurantId,
			@RequestParam Integer menuFoodId, ModelMap model,
			HttpServletRequest request) {
		if (request.getUserPrincipal() != null) {
			LOGGER.info("START CREATE FORM ADD FOOD!");
			FoodDto food = null;
			try {
				food = new FoodDto();
				food.setRestaurantId(restaurantId);
				food.setRestaurantName(iRestaurantManager.getRestaurantNameByRestaurantId(restaurantId));
				food.setMenuFoodId(menuFoodId);
				food.setMenuFoodName(iMenuFood.getMenuFoodNameByMenuFoodId(menuFoodId));
				model.addAttribute("food", food);
			} catch (Exception e) {
				LOGGER.error("ERROR WHEN CREATE FORM ADD FOOD!");
			}
			LOGGER.info("END CREATE FORM ADD FOOD!");
			return "/food/addfood";
		} else {
			return "redirect:/adminaccount/login";
		}
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	public String addFood(@ModelAttribute FoodDto food,
			RedirectAttributes redirectAttrs, HttpServletRequest request) {
		if (request.getUserPrincipal() != null) {
			LOGGER.info("START ADD NEW FOOD!");
			try {
				food.setCreateId(request.getUserPrincipal().getName());
				iFood.addFood(food);
				redirectAttrs.addAttribute("restaurantId",
						food.getRestaurantId());
				redirectAttrs.addAttribute("menuFoodId", food.getMenuFoodId());
			} catch (Exception e) {
				LOGGER.error("ERROR WHEN ADD NEW FOOD!", e);
			}
			LOGGER.info("END ADD NEW FOOD!");
		}
		return request.getUserPrincipal().getName() != null ? "redirect:/food/list"
				: "redirect:/adminaccount/login";
	}

	@RequestMapping(value = "/detail", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	public String detailFood(@RequestParam Integer restaurantId,
			@RequestParam Integer menuFoodId, @RequestParam Integer foodId,
			ModelMap model) {
		LOGGER.info("START VIEW DETAIL FOOD!");
		try {
			model.addAttribute("food", iFood.viewFood(foodId));
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN VIEW DETAIL FOOD!", e);
		}
		LOGGER.info("END VIEW DETAIL FOOD!");
		return "food/detailfood";
	}

	@RequestMapping(value = "/formupdate", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	public String createFormUpdate(@RequestParam Integer restaurantId,
			@RequestParam Integer menuFoodId, @RequestParam Integer foodId,
			ModelMap model, HttpServletRequest request) {
		if (request.getUserPrincipal() != null) {
			LOGGER.info("START CREATE FORM UPDATE FOOD!");
			try {
				model.addAttribute("food", iFood.viewFood(foodId));
			} catch (Exception e) {
				LOGGER.error("ERROR WHEN CREATE FORM UPDATE FOOD!");
			}
			LOGGER.info("END CREATE FORM UPDATE FOOD!");
			return "food/updatefood";
		} else {
			return "redirect:/adminaccount/login";
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	public String update(@ModelAttribute FoodDto food,
			RedirectAttributes redirectAttrs, HttpServletRequest request) {
		if (request.getUserPrincipal() != null) {
			LOGGER.info("START UPDATE FOOD!");
			try {
				food.setUpdateId(request.getUserPrincipal().getName());
				iFood.updateFood(food);
				redirectAttrs.addAttribute("restaurantId",
						food.getRestaurantId());
				redirectAttrs.addAttribute("menuFoodId", food.getMenuFoodId());
			} catch (Exception e) {
				LOGGER.error("ERROR WHEN UPDATE FOOD!", e);
			}
			LOGGER.info("END UPDATE FOOD!");
			return "redirect:/food/list";
		} else {
			return "redirect:/adminaccount/login";
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	public String deleteFood(@RequestParam Integer restaurantId,
			@RequestParam Integer menuFoodId, @RequestParam Integer foodId,
			RedirectAttributes redirectAttrs, HttpServletRequest request) {
		if (request.getUserPrincipal() != null) {
			LOGGER.info("START DELETE FOOD!");
			try {
				iFood.deleteFood(menuFoodId, foodId);
				redirectAttrs.addAttribute("restaurantId", restaurantId);
				redirectAttrs.addAttribute("menuFoodId", menuFoodId);
			} catch (Exception e) {
				LOGGER.error("ERROR WHEN DELETE FOOD!", e);
			}
			LOGGER.info("END DELETE FOOD!");
			return "redirect:/food/list";
		} else {
			return "redirect:/adminaccount/login";
		}
	}

	// @RequestMapping(value = "/delete", method = RequestMethod.POST)
	// @ResponseBody public String deleteFood(@RequestParam Integer menuFoodId,
	// @RequestParam Integer foodId) {
	// LOGGER.info("START DELETE FOOD!");
	// Boolean flag = true;
	// try {
	// iFood.deleteFood(menuFoodId, foodId);
	// } catch (Exception e) {
	// flag = false;
	// LOGGER.error("ERROR WHEN DELETE FOOD!", e);
	// }
	// LOGGER.info("END DELETE FOOD!");
	// return flag.toString();
	// }

	@RequestMapping(value = "/checkexistedfood", method = RequestMethod.POST)
	@ResponseBody
	public String checkExistedFood(@RequestParam Integer menuFoodId,
			@RequestParam String foodName) {
		LOGGER.info("START CHECK EXISTED FOOD NAME!");
		Boolean flag = null;
		try {
			flag = iFood.checkExistedFood(menuFoodId, foodName);
		} catch (Exception e) {
			LOGGER.error("ERROR CHECK EXISTED FOOD NAME!", e);
		}
		LOGGER.info("END CHECK EXISTED FOOD NAME!");
		return flag.toString();
	}

}
